package Lee_code;

import sun.reflect.generics.tree.Tree;

/**
 * @author blh
 * @date 2024年07月09日 13:51
 */
public class Lee_114_79 {
    public void flatten(TreeNode root) {
        firstBianli(root);

    }

    private void firstBianli(TreeNode root) {
        // 对根节点操作
        if (root.left != null) {
            firstBianli(root.left);
        }
        if (root.right != null) {
            firstBianli(root.right);
        }
        if (root.left!=null && root.right!=null) {
            TreeNode tempRight = root.right;
            root.right = root.left;
            TreeNode tempRoot = root;
            while (tempRoot.right!=null){
                tempRoot = tempRoot.right;
            }
            root.left = null;
            tempRoot.right = tempRight;
        } else if (root.left != null) {
            root.right = root.left;
            root.left = null;
        }
    }
}
